import { useI18n } from '@/hooks/web/useI18n'
import { reactive } from 'vue'
import { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { useValidator } from '@/hooks/web/useValidator'
const { t } = useI18n()
import { ElTag } from 'element-plus'
import { SchemaConfig } from '@/api/global/types'
const { required } = useValidator()

interface Option {
  label: string
  value: string | number // 具体取决于你的数据
}

// 通用选项生成函数
const createOptions = (enumObject: { [key: string]: string }): Option[] => {
  return Object.keys(enumObject).map((key) => ({
    label: enumObject[key],
    value: parseInt(key, 10)
  }))
}

export default function (schemaConfig: SchemaConfig) {
  const statusOption = reactive(createOptions(schemaConfig.fieldEnum?.status || {}))
  const crudSchemas = reactive<CrudSchema[]>([
    {
      field: 'selection',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      },
      table: {
        type: 'selection'
      }
    },
    {
      field: 'id',
      label: t('logisticsCompany.id'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'name',
      label: t('logisticsCompany.name'),
      form: {
        component: 'Input',
        formItemProps: {
          rules: [required()]
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'status',
      label: t('logisticsCompany.status'),
      form: {
        component: 'Select',
        componentProps: {
          options: statusOption
        }
      },
      search: {
        component: 'Select',
        componentProps: {
          options: statusOption,
          style: {
            width: '100%'
          }
        }
      },
      detail: {
        slots: {
          default: (data: any) => {
            return (
              <ElTag type={data.status === 0 ? 'warning' : 'success'}>{data.status_value}</ElTag>
            )
          }
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return (
              <ElTag type={data.row.status === 0 ? 'warning' : 'success'}>
                {data.row.status_value}
              </ElTag>
            )
          }
        }
      }
    },
    {
      field: 'company_type_value',
      label: '公司类型',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'parent_name',
      label: '所属的货代/物流公司',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'create_time',
      label: t('logisticsCompany.create_time'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'creator',
      label: t('logisticsCompany.creator'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        slots: {
          default: (data: any) => {
            return data.creator_value
          }
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.creator_value
          }
        }
      }
    },
    {
      field: 'update_time',
      label: t('logisticsCompany.update_time'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'updater',
      label: t('logisticsCompany.updater'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        hidden: true
      },
      detail: {
        slots: {
          default: (data: any) => {
            return data.updater_value
          }
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.updater_value
          }
        }
      }
    },
    {
      field: 'action',
      width: (schemaConfig.actionButtonsTemplate?.length || 3) * 60 + 20 + 'px',
      label: t('tableDemo.action'),
      fixed: 'right',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return schemaConfig.actionButtonsTemplate?.template(data) ?? null
          }
        }
      }
    }
  ])

  return {
    crudSchemas
  }
}
